

package com.hexinfo.dmpro.dev.data.transfer.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.data.transfer.common.common.manager.AgentManager;
import com.hexinfo.dmpro.dev.data.transfer.model.DevDataExportList;
import com.hexinfo.dmpro.dev.data.transfer.service.IDevDataExportListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.nio.file.Files;
import java.util.List;


/**
 * 表导出任务集合表
 *
 * @author yyh.huang
 * @date 2023-10-25 19:17:01
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/dev/data/export/list")
@Api(value = "devdataexportlist", tags = "表导出任务集合表管理")
public class DevDataExportListController {

    private final IDevDataExportListService devDataExportListService;

    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param devDataExportList 表导出任务集合表
     *                          WrapperEnhancer.like(devDataExportList); //自动拼接存在值sql的方法
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public ResponseEntity<Object> getDevDataExportListPage(Page page, DevDataExportList devDataExportList) {
        return ResponseEntity.ok(devDataExportListService.queryList(page, devDataExportList));
    }

    @ApiOperation("导出任务集合表清单")
    @GetMapping(value = "/download")
    public ResponseEntity<Resource> download(DevDataExportList devDataExportList) throws Exception {
        String downloadFile = devDataExportListService.download(devDataExportList);
        byte[] bytes = Files.readAllBytes(new File(downloadFile).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=文件.xls")
                .body(bar);
    }

    @ApiOperation(value = "开发数据导出执行完成更新清单信息", notes = "开发数据导出执行完成更新清单信息")
    @PostMapping
    @AnonymousAccess
    public ResponseEntity<Object> updateStatus(@RequestBody DevDataExportList devDataExportList) {
        log.info("更新开发数据导出清单信息：{}", devDataExportList.toString());
        devDataExportListService.updateStatus(devDataExportList);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "开发数据导出清单重跑", notes = "开发数据导出清单重跑")
    @PutMapping("/restart/{ids}")
    @AnonymousAccess
    public ResponseEntity<Object> restart(@PathVariable("ids") @NotNull List<String> ids) {
        log.info("开发数据导出清单重跑：{}", StrUtil.join(",", ids));
        devDataExportListService.restart(ids);
        return ResponseEntity.ok("ok");
    }

}
